package com.fit.utils.se;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;


/**
 * 生成随机id(还不太成熟)
 * 
 * @author Administrator
 * 
 */
public class SequenceUtil {

	/**
	 * 通过时间戳和固定字符串乱序获得id
	 * 
	 * @return
	 */
	private static String getIdByTime() {
		String tip = "abcdefghijklmn";

		String str13 = String.valueOf(System.currentTimeMillis());
		List<String> lt13 = new ArrayList<String>();
		lt13.addAll(Arrays.asList(str13.trim().split("")));
		lt13.remove(0);

		Collections.shuffle(lt13);
		for (int i = lt13.size(); i < 20; i++) {
			Random random = new Random();
			int index = random.nextInt(tip.length());
			char c1 = tip.charAt(index);
			String str = String.valueOf(c1);
			lt13.add(str);
			Collections.shuffle(lt13);
		}
		String id = "";
		for (String string : lt13) {
			id += string;
		}
		return id;
	}

	/**
	 * 获取多个id
	 * 
	 * @param sum
	 *            需要的id个数
	 * @return
	 */
	public static String[] getId(int sum) {
		String[] ids = new String[sum];
		for (int i = 0; i < sum; i++) {
			ids[i] = getIdByTime();
		}
		return ids;
	}

	/**
	 * 获取单个id
	 * 
	 * @return
	 */
	public static String getId() {
		return getIdByTime();
	}
	
	
	


	/**
	 * 获取系统流水号(由于长度问题，并不能保证返回的数据唯一)
	 * @param length 指定流水号长度
	 * @param isNumber  指定流水号是否全由数字组成
	 */
	public static String sysJournalNo(int length, boolean isNumber) {
		String uuid = IdentitiesUtil.uuid();
		if (uuid.length() > length) {
			uuid = uuid.substring(0, length);
		} else if (uuid.length() < length) {
			for (int i = 0; i < length - uuid.length(); i++) {
				uuid = uuid + Math.round(Math.random() * 9);
			}
		}
		if (isNumber) {
			return uuid.replaceAll("a", "1").replaceAll("b", "2").replaceAll("c", "3").replaceAll("d", "4").replaceAll("e", "5").replaceAll("f", "6");
		} else {
			return uuid;
		}
	}
}